package com.example.elmspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.elmspringboot.controller.utils.Result;
import com.example.elmspringboot.domain.Admin;
import com.example.elmspringboot.service.AdminService;
import com.example.elmspringboot.service.Impl.AdminServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Tag(name = "AdminController", description = "商家管理")
@RestController
@RequestMapping("/admins")
public class AdminController {
    @Resource
    private AdminService adminService;

    @Operation(summary = "获取商家列表",description = "需要登录后访问")
    @GetMapping
    public Result getAllAdmin(){
       // List<Admin> allAdmin = adminService.getAllAdmin();
        return new Result(true,adminService.list());

    }
    @Operation(summary = "通过id获取商家",description = "需要登录后访问")
    @GetMapping("/{id}")
    public Admin getAdminbyId(@PathVariable Integer id){
        return adminService.getById(id);
    }
    @Operation(summary = "新增商家",description = "需要登录后访问")
    @PostMapping
    public Result saveAdmin(@RequestBody Admin admin){
        Boolean flag = adminService.save(admin);
        String msg=flag?"success":"fail";
        return new Result(flag,msg);
    }
    @Operation(summary = "删除商家",description = "需要登录后访问")
    @DeleteMapping("/{id}")
    public Result deleteAdmin(@PathVariable Integer id){
        Boolean flag = adminService.removeById(id);
        String msg=flag? "success":"fail";
        return new Result(flag,msg);
    }
    @Operation(summary = "更新指定id品牌信息")
    @PutMapping//put真正的目的其实是执行大规模的替换操作
    public Result updateAdmin(@RequestBody Admin admin){
        Boolean flag = adminService.updateById(admin);
        String msg=flag?"success":"fail";
        return new Result(flag,msg);
    }

    @Operation(summary = "登录")
    @PostMapping("/login")
    public Result login(@RequestParam String username,@RequestParam String password) {
        Boolean flag = adminService.login(username, password);
        String msg="fail";
        if(flag){
            LambdaQueryWrapper<Admin> wrapper=new LambdaQueryWrapper<>();
            Admin admin = adminService.getOne(wrapper.eq(username != null, Admin::getAdminName, username));
            msg="success";
            return new Result(true,admin,msg);
        }
        return new Result(false,null,msg);
    }
    @Operation(summary = "注册")
    @PostMapping("/{username}/{password}")
    public Result register(@PathVariable String username,@PathVariable String password) {
        Boolean flag=adminService.register(username,password);
        if(flag){
            return new Result(true,null,"注册成功");
        }
        else{
            return new Result(false,null,"注册失败");
        }
    }
}
